var axis= { categories: ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']};
var axis2={categories:null};
function showOverview(){
	$("#mainForm").html("<div id='lineWeekChart'></div><div id='lineMonthChart'></div><div id='onlieChart'></div>");
	$.ajax({
		url:"count/monthCount.do",
		dataType:"json",
		success:function(data){
			if(data.success){
				var charts=data.result;
				var labels=data.attribute['labels'];
				var month=data.attribute['month'];
				axis2.categories=labels;
				setMonthLineChart(month+"月",charts,axis2,'lineMonthChart');
			}
		}
	});
	$.ajax({
		url:"count/weekCount.do",
		dataType:"json",
		success:function(data){
			if(data.success){
				var charts=data.result;
				setLineChart("本周",charts,axis,'lineWeekChart');
			}
		}
	});
	showOnlineChart();
}
function showOnlineChart(){
	  Highcharts.setOptions({
            global: {
                useUTC: false
            }
        });
    
         chart = new Highcharts.Chart({
            chart: {
                renderTo: 'onlieChart',
                type: 'spline',
                marginRight: 10,
                events: {
                    load: function() {
                        // set up the updating of the chart each second
                        var series = this.series[0];
                        setInterval(function() {
                            $.ajax({
                            	url:"count/onlineCount.do?ts="+new Date().getTime(),
                            	dataType:"text",
                            	success:function(count){
                            		var y=parseInt(count);
                            		 var x = (new Date()).getTime(); // current time
                            		series.addPoint([x, y], true, true);
                            	}
                            });
                        }, 5000);
                    }
                }
            },
            title: {
                text: '在线人数-动态数据'
            },
            xAxis: {
                type: 'datetime',
                tickPixelInterval: 150
            },
            yAxis: {
                title: {
                    text: '在线人数'
                },
                plotLines: [{
                    value: 0,
                    width: 1,
                    color: '#808080'
                }]
            },
            tooltip: {
                formatter: function() {
                        return '<b>'+ this.series.name +'</b><br/>'+
                        Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) +'<br/>'+
                        Highcharts.numberFormat(this.y, 2);
                }
            },
            legend: {
                enabled: false
            },
            exporting: {
                enabled: false
            },
            series: [{
                name: '用户数',
                data: (function() {
                    // generate an array of random data
                    var data = [],
                        time = (new Date()).getTime(),
                        i;
    
                    for (i = -5; i <= 0; i++) {
                        data.push({
                            x: time + i * 1000,
                            y: 1
                        });
                    }
                    return data;
                })()
            }]
        });

}
function showUnitCount(){
	$("#mainForm").html("<div id='pieChart'></div><div id='lineWeekChart'></div>");
	$.ajax({
		url:"count/lineChart.do?type=Unit",
		dataType:"json",
		success:function(data){
			if(data.success){
				var charts=data.result;
				setLineChart("栏目",charts,axis,'lineWeekChart');
			}
		}
	});
	$.ajax({
		url:"count/pieChart.do?type=Unit",
		dataType:"json",
		success:function(data){
			if(data.success){
				var charts=data.result;
				setPieChart("栏目",charts);
			}
		}
	});
}
function showCategoryCount(){
	$("#mainForm").html("<div id='pieChart'></div><div id='lineWeekChart'></div>");
	$.ajax({
		url:"count/lineChart.do?type=Category",
		dataType:"json",
		success:function(data){
			if(data.success){
				var charts=data.result;
				
				setLineChart("类别",charts,axis,'lineWeekChart');
			}
		}
	});
	$.ajax({
		url:"count/pieChart.do?type=Category",
		dataType:"json",
		success:function(data){
			if(data.success){
				var charts=data.result;
				setPieChart("类别",charts);
			}
		}
	});
}
function showArticleCount(){
	$("#mainForm").html("<div id='pieChart'></div><div id='lineWeekChart'></div>");
	$.ajax({
		url:"count/lineChart.do?type=Article",
		dataType:"json",
		success:function(data){
			if(data.success){
				var charts=data.result;
				setLineChart("文章Top10",charts,axis,'lineWeekChart');
			}
		}
	});
	$.ajax({
		url:"count/pieChart.do?type=Article",
		dataType:"json",
		success:function(data){
			if(data.success){
				var charts=data.result;
				setPieChart("文章Top10",charts);
			}
		}
	});
}
function setPieChart(type,data){
	var chart = new Highcharts.Chart({
		chart: {
		    renderTo: 'pieChart',
		    plotBackgroundColor: null,
		    plotBorderWidth: null,
		    plotShadow: false,
		     width:800
		},
		title: {
		    text: type+'热度统计'
		},
		tooltip: {
			 pointFormat: '{series.name}: <b>{point.percentage}%</b>',
			percentageDecimals: 1
		},
		plotOptions: {
		    pie: {
		      allowPointSelect: true,
                    cursor: 'pointer',
                    dataLabels: {
                        enabled: true
                    },
                    showInLegend: true
		    }
		},
		series: [{
		    type: 'pie',
		    name: '点击量比',
		    data:data
		}]
	});
}
function setMonthLineChart(type,charts,xaxis){
var caption=type+"点击趋势";
var chart = new Highcharts.Chart({
	chart: {
	    renderTo: 'lineMonthChart',
	    type: 'spline',
	    width:800
	},
	title: {
	    text: caption
	},
	subtitle: {
	    text: '信息统计'
	},
	xAxis:xaxis,
	yAxis: {
	    title: {
	        text: '点击数'
	    }
	},
	tooltip: {
	    enabled: true,
	    formatter: function() {
	        return '<b>'+ this.series.name +this.x +'日</b><br/>点击: '+ this.y +"次";
	    }
	},
	plotOptions: {
	    line: {
	        dataLabels: {
				enabled: true
	        },
	        enableMouseTracking: true
	    }
	},
	series:charts
});
//line chart end
}
function setLineChart(type,charts,xaxis,renderTo){
var caption=type+"点击趋势";
var chart = new Highcharts.Chart({
	chart: {
	    renderTo: renderTo,
	    type: 'spline',
	    width:800
	},
	title: {
	    text: caption
	},
	subtitle: {
	    text: '信息统计'
	},
	xAxis:xaxis,
	yAxis: {
	    title: {
	        text: '点击数'
	    }
	},
	tooltip: {
	    enabled: true,
	    formatter: function() {
	        return '<b>'+ this.series.name +'</b><br/>'+this.x +': '+ this.y +"次";
	    }
	},
	plotOptions: {
	    line: {
	        dataLabels: {
				enabled: true
	        },
	        enableMouseTracking: true
	    }
	},
	series:charts
});
//line chart end
}